<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <!-- <a-form-item label="行号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['rowNum']" placeholder="请输入行号"></a-input>
        </a-form-item> -->
        <a-form-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielType']" placeholder="请输入物料类型"></a-input>
        </a-form-item>
        <a-form-item label="料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['materielCode']"
            :trigger-change="true"
            org-fields="id,materiel_code,product_name,unit"
            dest-fields="materielId,materielCode,materielName,countUnit"
            code="materiel_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="物料" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielName']" placeholder="请输入物料"></a-input>
        </a-form-item>
        <a-form-item label="物料规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielGauge']" placeholder="请输入物料规格"></a-input>
        </a-form-item>
        <a-form-item label="物料阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['materielGrade']" triggerChange dictCode="produce_grade" placeholder="请选择物料阶别"/>
        </a-form-item>
        <a-form-item label="点位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['spot']" placeholder="请输入点位"></a-input>
        </a-form-item>
        <a-form-item label="用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['quantity']" placeholder="请输入用量"></a-input>
        </a-form-item>
        <a-form-item label="反面点位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['inverseSpot']" placeholder="请输入反面点位"></a-input>
        </a-form-item>
        <a-form-item label="反面用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['inverseQuantity']" placeholder="请输入反面用量"></a-input>
        </a-form-item>
        <a-form-item label="正面点位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['frontSpot']" placeholder="请输入正面点位"></a-input>
        </a-form-item>
        <!-- <a-form-item label="正面用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['frontQuantity']" placeholder="请输入正面用量"></a-input>
        </a-form-item> -->
        <a-form-item label="需求数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['requireNum']" placeholder="请输入需求数量"></a-input>
        </a-form-item>
        <a-form-item label="计量单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['countUnit']" placeholder="请输入计量单位"></a-input>
        </a-form-item>
        <!-- <a-form-item label="项目类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectSort']" placeholder="请输入项目类别"></a-input>
        </a-form-item>
        <a-form-item label="工序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['process']" placeholder="请输入工序"></a-input>
        </a-form-item>
        <a-form-item label="顺序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['sequence']" placeholder="请输入顺序"></a-input>
        </a-form-item>
        <a-form-item label="提货数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['pickupNum']" placeholder="请输入提货数量"></a-input>
        </a-form-item>
        <a-form-item label="承诺数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['commitNum']" placeholder="请输入承诺数量"></a-input>
        </a-form-item> -->
        <a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['factoryCode']"
            :trigger-change="true"
            org-fields="id,fac_code,fac_nameone,store_name"
            dest-fields="factoryId,factoryCode,factoryName,storageSite"
            code="factorysite_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="工厂名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['factoryName']" placeholder="请输入工厂名称"></a-input>
        </a-form-item>
        <a-form-item label="库存地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['storageSite']" placeholder="请输入库存地点"></a-input>
        </a-form-item>
       <!-- <a-form-item label="库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['stockNum']" placeholder="请输入库存数量"></a-input>
        </a-form-item>
        <a-form-item label="批次" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['batchNum']" placeholder="请输入批次"></a-input>
        </a-form-item>
        <a-form-item label="反冲" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['inverseCharge']" :trigger-change="true" dictCode="yn" placeholder="请选择反冲"/>
        </a-form-item>
        <a-form-item label="已删除" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['haveDelete']" :trigger-change="true" dictCode="yn" placeholder="请选择已删除"/>
        </a-form-item>
        <a-form-item label="最后发货" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['lastDelivery']" :trigger-change="true" dictCode="yn" placeholder="请选择最后发货"/>
        </a-form-item>
        <a-form-item label="需求日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择需求日期" v-decorator="['requireDate']" :trigger-change="true" style="width: 100%"/>
        </a-form-item> -->
        <a-form-item v-show="false" label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['materielId']" placeholder="请输入物料ID"></a-input>
        </a-form-item>
        <a-form-item v-show="false" label="工厂ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['factoryId']" placeholder="请输入工厂ID"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesProduceItemModal",
    components: {
      JDate,
      JDictSelectTag,
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/order/mesOrderProduce/addMesProduceItem",
          edit: "/order/mesOrderProduce/editMesProduceItem",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','updateBy','updateTime','sysOrgCode','materielId','materielType','materielCode','materielName','materielGauge','materielGrade','requireNum','countUnit','projectSort','process','sequence','pickupNum','commitNum','factoryId','factoryCode','factoryName','storageSite','stockNum','batchNum','inverseCharge','haveDelete','lastDelivery','requireDate','proorderId','rowNum','spot','quantity','inverseSpot','inverseQuantity','frontSpot','frontQuantity'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['proorderId'] = this.mainId
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','sysOrgCode','materielId','materielType','materielCode','materielName','materielGauge','materielGrade','requireNum','countUnit','projectSort','process','sequence','pickupNum','commitNum','factoryId','factoryCode','factoryName','storageSite','stockNum','batchNum','inverseCharge','haveDelete','lastDelivery','requireDate','proorderId','rowNum','spot','quantity','inverseSpot','inverseQuantity','frontSpot','frontQuantity'))
      },


    }
  }
</script>
